package org.springframework.core.io;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Name DefaultResourceLoader
 * @Description: 资源加载器 [实现] 类
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-03-15 15:20:14
 **/
public class DefaultResourceLoader implements ResourceLoader {
    private static final String CLASSPATH_URL_PREFFIX = "classpath:";

    @Override
    public Resource getResource(String location) {
        if (location.startsWith(CLASSPATH_URL_PREFFIX)) {
            // 1. 加载 ClassPath 下的资源
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFFIX.length()));
        } else {
            try {
                // 2. 尝试作为 URL 来加载
                URL url = new URL(location);
                return new UrlResource(url);
            } catch (MalformedURLException e) {
                // 3. 作为文件系统下的资源来加载
                return new FileSystemResource(location);
            }
        }
    }
}

